package com.zyh.util;


import com.zyh.util.ESBQRCB01.*;
import com.zyh.util.ESBQRCB02.ESBQRCBBody02;
import com.zyh.util.ESBQRCB02.ESBQRCBHead02;
import com.zyh.util.ESBQRCB02.ESBQRCBMap02;
import com.zyh.util.ESBQRCB02.EsbResponseQRCB02;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

public class Dom23 {


    public static EsbResponseQRCB01 doESBResponseObj01(String xmlData,String serialNo) throws DocumentException {

        EsbResponseQRCB01 esbResponseQRCB01 = new EsbResponseQRCB01();

        Document doc =  DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();
        String RetCd = "";
        //只返回 SysHead 内容
        Iterator iterSysHead  =rootElt.elementIterator("SysHead");
        while(iterSysHead.hasNext()){

            Element reEless = (Element) iterSysHead.next();

            String BsnSrlNo = reEless.elementTextTrim("BsnSrlNo");

            Iterator iterRetInfArry  =reEless.elementIterator("RetInfArry");

            while (iterRetInfArry.hasNext()){

                Element reElessRetInfArry = (Element) iterRetInfArry.next();

                 RetCd = reElessRetInfArry.elementTextTrim("RetCd");
                String RetMsg = reElessRetInfArry.elementTextTrim("RetMsg");



                esbResponseQRCB01.getEsbqrcbHead().setIBSReturnMsg(RetMsg);//网银返回信息
                esbResponseQRCB01.getEsbqrcbHead().setIBSReturnCode(RetCd);//网银返回码

            }

            esbResponseQRCB01.getEsbqrcbHead().setIBSJnlNo(BsnSrlNo);//网银交易流水


        }
        List<ESBQRCBMap01> listMaps = new ArrayList<>();
        //如果 RetCd != 000000 就不要解析 boby

        if(  "000000".equals(RetCd)){




        // 只返回 Body 内容
        Iterator itersBody =  rootElt.elementIterator("Body");

        ESBQRCBList01 esbqrcbList01 = new ESBQRCBList01();


        while(itersBody.hasNext()){

            Element reEless = (Element) itersBody.next();

            String StCd = reEless.elementTextTrim("StCd");
            String AcctStDsc = reEless.elementTextTrim("AcctStDsc");

            DecimalFormat format = new DecimalFormat("0.00");


            String AcctBal = reEless.elementTextTrim("AcctBal");
            String AvlblBal = reEless.elementTextTrim("AvlblBal");
            AcctBal = format.format(new BigDecimal(AcctBal));
            AvlblBal = format.format(new BigDecimal(AvlblBal));
            String CstNo = reEless.elementTextTrim("CstNo");
            String CstNm = reEless.elementTextTrim("CstNm");



            //Map标签
            ESBQRCBMap01 esbqrcbMap01 = new ESBQRCBMap01();

            esbqrcbMap01.setStCd(StCd);
            esbqrcbMap01.setAcctStDsc(AcctStDsc);
            esbqrcbMap01.setAcctBal(AcctBal);
            esbqrcbMap01.setCstNm(CstNm);
            esbqrcbMap01.setCstNo(CstNo);
            esbqrcbMap01.setAvlblBal(AvlblBal);



            listMaps.add(esbqrcbMap01);

        }
        }

        esbResponseQRCB01.getEsbqrcbHead().setSerialNo(serialNo);//返回请求方的交易流水号
        esbResponseQRCB01.getEsbQRCBBody01().setEsbqrcbList01(listMaps);

        return esbResponseQRCB01;
    }

    public static EsbResponseQRCB02 doESBResponseObj02(String xmlData,String serialNo) throws DocumentException {

        EsbResponseQRCB02 esbResponseQRCB02 = new EsbResponseQRCB02();

        Document doc =  DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();

        ESBQRCBHead02 esbqrcbHead02 = new ESBQRCBHead02();
        ESBQRCBBody02 esbqrcbBody02 = esbResponseQRCB02.getEsbQRCBBody02();
        //只返回 SysHead 内容
        Iterator iterSysHead  =rootElt.elementIterator("SysHead");
        while(iterSysHead.hasNext()){

            Element reEless = (Element) iterSysHead.next();

            String BsnSrlNo = reEless.elementTextTrim("BsnSrlNo");

            Iterator iterRetInfArry  =reEless.elementIterator("RetInfArry");

            while (iterRetInfArry.hasNext()){

                Element reElessRetInfArry = (Element) iterRetInfArry.next();

                String RetCd = reElessRetInfArry.elementTextTrim("RetCd");
                String RetMsg = reElessRetInfArry.elementTextTrim("RetMsg");
                esbqrcbHead02.setIBSReturnMsg(RetMsg);//网银返回信息
                esbqrcbHead02.setIBSReturnCode(RetCd);//网银返回码


            }

            esbqrcbHead02.setIBSJnlNo(BsnSrlNo);//网银交易流水


        }

        // 只返回 Body 内容
        Iterator itersBody =  rootElt.elementIterator("Body");

        ESBQRCBList01 esbqrcbList01 = new ESBQRCBList01();
        List<ESBQRCBMap02> listMaps = new ArrayList<>();

        while(itersBody.hasNext()) {

            Element reEless = (Element) itersBody.next();
            Iterator iterRetInfArry = reEless.elementIterator("AcctDtlArry");

            while (iterRetInfArry.hasNext()) {

                Element reElessiterRetInfArry = (Element) iterRetInfArry.next();



                String TxnCntprAcctNo = reElessiterRetInfArry.elementTextTrim("TxnCntprAcctNo");//交易对方账户
                String TxnCntprNm = reElessiterRetInfArry.elementTextTrim("TxnCntprNm");//交易对方Name
                String TxnTlr = reElessiterRetInfArry.elementTextTrim("TxnTlr");//交易柜员

                DecimalFormat decimalFormat = new DecimalFormat("0.00");

                String TxnAmt = reElessiterRetInfArry.elementTextTrim("TxnAmt");//交易金额

                //TxnAmt = decimalFormat.format(new BigDecimal(TxnAmt));
                String TxnDt1 = reElessiterRetInfArry.elementTextTrim("TxnDt1");//交易日期

                String Tm = reElessiterRetInfArry.elementTextTrim("Tm");//交易时间
                String Bal = reElessiterRetInfArry.elementTextTrim("Bal");//账户余额
               // Bal = decimalFormat.format(new BigDecimal(Bal));

                String QrySrlNo = reElessiterRetInfArry.elementTextTrim("QrySrlNo");//查询流水号—— 核心流水号

                String TxnDsc = reElessiterRetInfArry.elementTextTrim("TxnDsc");//交易描述——用途
                String RmkInf = reElessiterRetInfArry.elementTextTrim("RmkInf");//备注信息


                //Map标签
                ESBQRCBMap02 esbqrcbMap02 = new ESBQRCBMap02();


                esbqrcbMap02.setBsnTellNo(TxnTlr);
                esbqrcbMap02.setAmount(TxnAmt);
                esbqrcbMap02.setTransferDate(TxnDt1);
                esbqrcbMap02.setTransferTime(Tm);
                esbqrcbMap02.setBalance(Bal);
                esbqrcbMap02.setHostFlowNo(QrySrlNo);
                esbqrcbMap02.setPayUse(TxnDsc);
                esbqrcbMap02.setOppositeAccNo(TxnCntprAcctNo);
                esbqrcbMap02.setOppositeName(TxnCntprNm);
                esbqrcbMap02.setSummary(RmkInf);



                listMaps.add(esbqrcbMap02);

            }
        }


        esbqrcbHead02.setSerialNo(serialNo);//返回请求方的交易流水号
        esbqrcbBody02.setEsbqrcbList02(listMaps);

        esbResponseQRCB02.setEsbQRCBBody02(esbqrcbBody02);
        esbResponseQRCB02.setEsbqrcbHead02(esbqrcbHead02);
        return esbResponseQRCB02;
    }


    public static Map<String,String> doESBResponseObjSaveMoney(String xmlData,String uuid) throws DocumentException {

     Map<String,String> resultMap = new HashMap<>();

        Document doc =  DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();


        //只返回 SysHead 内容
        Iterator iterSysHead  =rootElt.elementIterator("SysHead");
        while(iterSysHead.hasNext()) {

            Element reEless = (Element) iterSysHead.next();

            String BsnSrlNo = reEless.elementTextTrim("BsnSrlNo");

            Iterator iterRetInfArry = reEless.elementIterator("RetInfArry");

            while (iterRetInfArry.hasNext()) {

                Element reElessRetInfArry = (Element) iterRetInfArry.next();

                String RetCd = reElessRetInfArry.elementTextTrim("RetCd");
                String RetMsg = reElessRetInfArry.elementTextTrim("RetMsg");

                resultMap.put("RetMsg", RetMsg);///网银返回信息
                resultMap.put("RetCd", RetCd);////网银返回码

                resultMap.put("uuid",uuid);
            }


        }



        return resultMap;
    }

    public static Map<String,Object> doESBResponseObjBig(String xmlData,String uuid) throws DocumentException {

        Map<String,Object> resultMap = new HashMap<>();

        Document doc =  DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();


        //只返回 SysHead 内容
        Iterator iterSysHead  =rootElt.elementIterator("SysHead");
        while(iterSysHead.hasNext()) {

            Element reEless = (Element) iterSysHead.next();

            String BsnSrlNo = reEless.elementTextTrim("BsnSrlNo");

            Iterator iterRetInfArry = reEless.elementIterator("RetInfArry");

            while (iterRetInfArry.hasNext()) {

                Element reElessRetInfArry = (Element) iterRetInfArry.next();

                String RetCd = reElessRetInfArry.elementTextTrim("RetCd");
                String RetMsg = reElessRetInfArry.elementTextTrim("RetMsg");

                resultMap.put("RetMsg", RetMsg);///网银返回信息
                resultMap.put("RetCd", RetCd);////网银返回码

                resultMap.put("uuid",uuid);
            }
            // 只返回 Body 内容
            Iterator itersBody = rootElt.elementIterator("Body");


            while (itersBody.hasNext()) {

                Element reElessBody = (Element) itersBody.next();

                String CnlSrlNo = reEless.elementText("CnlSrlNo");//渠道流水号


                resultMap.put("CnlSrlNo",CnlSrlNo);

            }

        }



        return resultMap;
    }

    public static String doESBResponseObjBigTaskLog(String xmlData,String uuid) throws DocumentException {

        Map<String, String> resultMap = new HashMap<>();

        String  srlst = "";

        Document doc = DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();


        // 只返回 Body 内容
        Iterator itersBody = rootElt.elementIterator("Body");


        while (itersBody.hasNext()) {

            Element reEless = (Element) itersBody.next();
            srlst = reEless.elementText("SrlSt");//流水状态1-交易成功    2-交易失败  3-处理中

        }
        return   srlst;
    }
    public static Map<String,String> doESBResponseObjBigLog(String xmlData,String uuid) throws DocumentException {

        Map<String, String> resultMap = new HashMap<>();

        Document doc = DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();

        //只返回 SysHead 内容
        Iterator iterSysHead  =rootElt.elementIterator("SysHead");
        while(iterSysHead.hasNext()) {

            Element reEless = (Element) iterSysHead.next();

            String BsnSrlNo = reEless.elementTextTrim("BsnSrlNo");

            Iterator iterRetInfArry = reEless.elementIterator("RetInfArry");

            while (iterRetInfArry.hasNext()) {

                Element reElessRetInfArry = (Element) iterRetInfArry.next();

                String RetCd = reElessRetInfArry.elementTextTrim("RetCd");
                String RetMsg = reElessRetInfArry.elementTextTrim("RetMsg");

                resultMap.put("RetMsg", RetMsg);///网银返回信息
                resultMap.put("RetCd", RetCd);////网银返回码

                resultMap.put("uuid",uuid);
            }


        }
        // 只返回 Body 内容
        Iterator itersBody = rootElt.elementIterator("Body");


        while (itersBody.hasNext()) {

            Element reEless = (Element) itersBody.next();
            String SrlSt = reEless.elementText("SrlSt");//流水状态1-交易成功    2-交易失败  3-处理中
            String CnlSrlNo = reEless.elementText("CnlSrlNo");//渠道流水号

            resultMap.put("SrlSt",SrlSt);
            resultMap.put("CnlSrlNo",CnlSrlNo);

        }
        return resultMap;
    }

    public static Xml01 toSpecialObj(String xmlData) throws DocumentException {

        Xml01 xml01 =  new Xml01();

        Document doc =  DocumentHelper.parseText(xmlData);
        Element rootElt = doc.getRootElement();

        Iterator iter =  rootElt.elementIterator("SysHead");

        while(iter.hasNext()){

            Element reEle = (Element) iter.next();

            String resCode = reEle.elementTextTrim("TxnDt");//交易吗

            if(!"27112020".equals(resCode)){
                String TxnDsc = reEle.elementTextTrim("TxnDsc");//交易描述
                //log  return

            }

            String SvcCd = reEle.elementTextTrim("SvcCd");
            xml01.setSvcCd(SvcCd);
        }

//        //AppHead
//        Iterator iters =  rootElt.elementIterator("AppHead");
//
//        //LocalHead
//        Iterator itersLoc =  rootElt.elementIterator("AppHead");

        // 只返回 Body 内容
        Iterator itersBody =  rootElt.elementIterator("Body");


        while(itersBody.hasNext()){

            Element reEless = (Element) itersBody.next();
            String StCd = reEless.elementTextTrim("StCd");
            String AcctStDsc = reEless.elementTextTrim("AcctStDsc");
            String CstNm = reEless.elementTextTrim("CstNm");
            String AcctOpnOtltsNm = reEless.elementTextTrim("AcctOpnOtltsNm");
            String IntRate = reEless.elementTextTrim("IntRate");
            String AcctBal = reEless.elementTextTrim("AcctBal");
            String AvlblBal = reEless.elementTextTrim("AvlblBal");
            String AcctOpnOtltsNo = reEless.elementTextTrim("AcctOpnOtltsNo");

            String OtltsCd = reEless.elementTextTrim("OtltsCd");

            //账户余额查询用账户号【解析传入】
            String PayerAcNO = reEless.elementTextTrim("PayerAcNO");
            xml01.setPayerAcNO(PayerAcNO);



            xml01.setCstNm(CstNm);
            xml01.setStCd(StCd);
            xml01.setAcctStDsc(AcctStDsc);
            xml01.setAcctOpnOtltsNm(AcctOpnOtltsNm);
            xml01.setAcctOpnOtltsNo(AcctOpnOtltsNo);
            xml01.setIntRate(IntRate);
            xml01.setAcctBal(AcctBal);
            xml01.setAvlblBal(AvlblBal);
            xml01.setOtltsCd(OtltsCd);


        }




        return xml01;
    }

    public static Xml23 test(String xmlData) throws DocumentException {

            Xml23 xml23 = new Xml23();

            Document doc =  DocumentHelper.parseText(xmlData);
            Element rootElt = doc.getRootElement();

            Iterator iter =  rootElt.elementIterator("SysHead");

            while(iter.hasNext()){

                Element reEle = (Element) iter.next();

                String resCode = reEle.elementTextTrim("TxnDt");//交易吗

                if(!"27112020".equals(resCode)){
                    String TxnDsc = reEle.elementTextTrim("TxnDsc");//交易描述
                    //log  return

                }

                String SvcCd = reEle.elementTextTrim("SvcCd");
                xml23.setSvcCd(SvcCd);
                xml23.setTxnCd(resCode);
            }

            //AppHead
            Iterator iters =  rootElt.elementIterator("AppHead");

            //LocalHead
            Iterator itersLoc =  rootElt.elementIterator("AppHead");

            //Body
            Iterator itersBody =  rootElt.elementIterator("Body");

            List<Xmlson> list = new ArrayList<Xmlson>();
            while(itersBody.hasNext()){


                Element items = (Element) itersBody.next();
                Iterator iterators = items.elementIterator("AcctDtlArry");
                while (iterators.hasNext()){
                    Xmlson xmlson = new Xmlson();
                    Element reEless = (Element) iterators.next();
                    String TxnCntprNm = reEless.elementTextTrim("TxnCntprNm");
                    String TxnCntprAcctNo = reEless.elementTextTrim("TxnCntprAcctNo");
                    xmlson.setTxnCntprNm(TxnCntprNm);
                    xmlson.setTxnCntprAcctNo(TxnCntprAcctNo);
//                    JSONObject jsonObject = new JSONObject();
//                    jsonObject.put("TxnCntprNm",TxnCntprNm);
                    list.add(xmlson);
                }

            }
            if(list.size() > 0){
                xml23.setItems(list);
            }

            return xml23;
        }

    public static ESBQRCB01AccTop doESBResponseAccTopObj(String xmlData) throws DocumentException {

        ESBQRCB01AccTop esbqrcb01AccTop = new ESBQRCB01AccTop();

        Document doc =  DocumentHelper.parseText(xmlData);

        Element rootElt = doc.getRootElement();



        // 只返回 Body 内容
        Iterator itersBody =  rootElt.elementIterator("Body");



        while(itersBody.hasNext()){

            Element reEless = (Element) itersBody.next();

            String AcctOpnOtltsNo = reEless.elementTextTrim("AcctOpnOtltsNo");
            String CstNm = reEless.elementTextTrim("CstNm");
            esbqrcb01AccTop.setAcctOpnOtltsNo(AcctOpnOtltsNo);
            //esbqrcb01AccTop.setAcctOpnOtltsNo(CstNm);

        }



        return esbqrcb01AccTop;
    }


    @Test
    public void Test(){


        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><service><SysHead><MsgLen>0377</MsgLen><TmlIndNo>000000</TmlIndNo><PvdrSrlNo>000602315</PvdrSrlNo><TxnDt>27112020</TxnDt><BsnSrlNo>C4012020201210150844137133470843</BsnSrlNo><SvcCd>300130001</SvcCd><ScnCd>23</ScnCd><TxnTm>150853</TxnTm><FileFlg></FileFlg><PvdrSysNo></PvdrSysNo><RetSt></RetSt><CnsmrSysNo>302300</CnsmrSysNo><RetInfArry><RetCd>000000</RetCd><RetMsg>O.K</RetMsg></RetInfArry></SysHead><AppHead><InstNo>003</InstNo><OtltsNo>10001</OtltsNo><UsrNo>8170004</UsrNo><CnlCd>Y</CnlCd></AppHead><LocalHead><TlrTp>0</TlrTp><AuthTlrNo>0000000</AuthTlrNo><OldAcctFlg>1</OldAcctFlg><TpInd>03</TpInd><ErrFlg></ErrFlg><AuthLvl></AuthLvl></LocalHead><Body><TxnBegNo>0002</TxnBegNo><RcrdNum>000000001</RcrdNum><AcctDtlArry><TxnDt1>20200109</TxnDt1><Tm>09:11:16</Tm><QrySrlNo>000038082</QrySrlNo><TxnCd>001045</TxnCd><TxnDsc>无折转客户帐   </TxnDsc><Ccy>CNY</Ccy><TxnAmt>        4600000.00 </TxnAmt><Bal>        9684378.90 </Bal><Otlts>01000</Otlts><TxnTlr>8170001</TxnTlr><PrmptCd>AP</PrmptCd><TxnCntprAcctNo>82011000000003124         </TxnCntprAcctNo><TxnCntprNm>海博青天钢结构有限责任公司                        </TxnCntprNm><CntprBnkNo>              </CntprBnkNo><RmkInf>   -                                            </RmkInf></AcctDtlArry></Body> <Body><TxnBegNo>0002</TxnBegNo><RcrdNum>000000001</RcrdNum><AcctDtlArry><TxnDt1>20200109</TxnDt1> <Tm>09:11:16</Tm><QrySrlNo>000038082</QrySrlNo><TxnCd>001045</TxnCd><TxnDsc>无折转客户帐</TxnDsc><Ccy>CNY</Ccy><TxnAmt>4600000.00</TxnAmt><Bal>9684378.90</Bal><Otlts>01000</Otlts><PrmptCd>AP</PrmptCd><TxnCntprAcctNo>82011000000003124</TxnCntprAcctNo><TxnCntprNm>海博青天钢结构有限责任公司</TxnCntprNm><CntprBnkNo></CntprBnkNo><RmkInf>-</RmkInf></AcctDtlArry></Body></service> ";


        EsbResponseQRCB02 esbResponseQRCB02 = null;
        try {
            esbResponseQRCB02 = doESBResponseObj02(str,"1");

            System.out.println(XstreamKit.vo2Xml(esbResponseQRCB02));
        } catch (DocumentException e) {
            e.printStackTrace();
        }


    }

}
